<?php
class PersonnelActController  extends Zone_Action{
	public function deleteAction(){
		$ids = get('ID',array(),true);

		if(count($ids)==0){
			return $this->setJSON(array(
				alert => 'Chưa chọn nhân viên ghi nào'
			));
		}

		foreach( $ids as $post_id ){	
			$post = $this->Model->fetchRow("SELECT * 
				FROM `personnels` 
				WHERE 
					`ID`='$post_id' 
					AND `is_draft`='no'");

			if( !$post ){
				continue;
			}			

			if( $post['photo'] ){
				@unlink("files/personnel/{$post['photo']}");
			}
			
			$files = $this->Model->fetchAll("SELECT * 
				FROM `personnels_files` 
				WHERE `personnel_id`='$post_id'");
			$this->Model->delete('personnels_files',"`personnel_id`='$post_id'");
					
			foreach( $files as $file ){
				@unlink("files/personnel/{$file['name']}");
			}
			
			
			$this->Model->delete('personnels',"`ID`='$post_id'");
			$this->Model->delete('personnels_cvs',"`personnel_id`='$post_id'");
			$this->Model->delete('personnels_salarys',"`personnel_id`='$post_id'");
			$this->Model->delete('personnels_activities',"`personnel_id`='$post_id'");
			$this->Model->delete('personnels_govs_activities',"`personnel_id`='$post_id'");
			$this->Model->delete('personnels_govs',"`personnel_id`='$post_id'");
		}	

		return $this->setJSON(array(
			redirect => '#'.get('url','Personnel')
		));
	}
}	